<?php

namespace Common\Model;

/**
 * HotelEntityModel.class实例模型
 * 
 * @author songanwei
 * @date 2016-1-4
 */
class HotelEntityModel extends BaseModel {

    protected $trueTableName = 'tb_hotel_entity';

    /**
     * 返回客栈名称
     * @param integer $id 客栈ID
     * @return type
     */
    public function getName($id) {
        return $this->where(array('id' => $id))->getField('name');
    }

    /**
     * 获取所有活跃客栈ID
     * @modify trainyao 2016-05-20    创建
     * @return array   id数组
     */
    public function getAllId() {
        $data = $this->field('id,name')->select();
        return empty($data) ? array() : $data;
    }

    /* 添加客栈
     * @modify songanwei 2016-01-04
     * @modify zhangzhen 2017-01-05 添加结束时间
     * @modify songanwei 2017-05-11 新增 $areaCode $universalTime
     * @param type $name
     * @param type $cityId
     * @param type $districtId
     * @param type $enddate
     * @param type $status
     * @return type
     */
    public function addInfo($name, $cityId, $districtId, $enddate=null, $status = 0, $usage=1, $universalTime = '5' ,$communityno = null) {
        $options = array(
            'name' => $name,
            'city_id' => $cityId,
            'district_id' => $districtId,
            'status' => $status,
            'usage' => $usage,
            'enddate' => empty($enddate)?date('Y-m-d H:i:s', strtotime('+1 year')):$enddate,
            'universaltime_id' => $universalTime,
        );
        if ($enddate) {
            $options['enddate'] = $enddate;
        }
        if (!is_null($communityno)) {
            $options['communityno'] = $communityno;
        }
        return $this->add($options);
    }

    /**
     * 更新客栈到期日期
     * @modify songanwei 2016-03-17
     * @param integer $hotelId  客栈ID
     * @param date $date 到期日期
     * @return type
     */
    public function setDate($hotelId, $date, $usage = null, $roomLimit = null) {
		if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
			$option['id'] = array('in', $hotelId);
        }else{
			$option['id'] = $hotelId;
		}
		$data = array('enddate' => $date);
		if(isset($usage)){
		    $data['usage'] = $usage;
		}
        if(!is_null($roomLimit)){
            $data['roomlimit'] = $roomLimit;
        }
        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }
    public function getList($pageIndex, $pageSize, $hotelName = ''){
        $where = array();
        $where['status'] = 0;
        $where['enddate'] = array(
            array('gt', date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME'])),
            array('eq', '0000-00-00'),
            'or'
        );
        if(!empty($hotelName)){
            $where['name'] = array('like', '%'.$hotelName.'%');
        }
        $result = $this->where($where)->limit(($pageIndex-1)*$pageSize, $pageSize)->order(array('id'=>'desc'))->select();   
        return $result;     
    }
    public function getRows($hotelName = ''){
        $where = array();
        $where['status'] = 0;
        $where['enddate'] = array(
            array('gt', date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME'])),
            array('eq', '0000-00-00'),
            'or'
        );
        if(!empty($hotelName)){
            $where['name'] = array('like','%'.$hotelName.'%');
        }
        return $this->where($where)->count();        
    }
    /**
     * 返回客栈名称
     * @param integer $id 客栈ID
     * @return type
     */
    public function getNameByIds($ids) {
        if(empty($ids)){
            return array();
        }
        $where = array(
            'id' => array('in',$ids),
            'isdelete' => NOT_DELETE,
        );
        return $this->field('id,name')->where($where)->select();
    }

    /**
     * 客栈启用或禁用 通过时间判断是否失效 用updatetime缓存当前到期时间
     * @modify zhangzhen 2016-12-29
     * @param integer $hotelId客栈ID(s)
     * @param date $enddate 到期日期
     * @param date $updatetime 更新时间(暂存到期时间)
     * @return type
     */
    public function setActive($hotelId, $enddate, $updatetime = null) {
        if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
            $option['id'] = array('in', $hotelId);
        }else{
            $option['id'] = $hotelId;
        }
        $data = array(
                'enddate' => $enddate,
            );
        if ($updatetime) {
            $data['updatetime'] = $updatetime;
        }
        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }

}
